package com.firefly.app.cordova.plugins;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;

import android.app.Activity;
import android.content.Intent;

import com.firefly.app.biz.yhc.dto.ResponseDTO;
import com.firefly.app.biz.yhc.dto.UserChangePwdRequest;
import com.firefly.app.biz.yhc.dto.UserFindPwdRequest;
import com.firefly.app.biz.yhc.dto.UserLoginRequest;
import com.firefly.app.biz.yhc.dto.UserLogoutRequest;
import com.firefly.app.biz.yhc.dto.UserRegRequest;
import com.firefly.app.biz.yhc.dto.UserValidCodeRequest;
import com.firefly.app.biz.yhc.service.YHCService;
import com.firefly.app.biz.yhc.service.YHCServiceImpl;
import com.firefly.mobile.app.context.SytApplication;
import com.firefly.mobile.app.util.SytLog;
import com.umeng.analytics.MobclickAgent;

public class YHCPlugin  extends CordovaPlugin implements CordovaInterface{
		private final ExecutorService threadPool = Executors.newCachedThreadPool(); 
	 
	    /** 
	     * 注意 构造方法不能为 
	     *  
	     * HomePlugin(){}   
	     * 可以不写或者 定义为如下 
	     *   
	     */  
	    public YHCPlugin() {  
	    }  
	    
	    private YHCService yhcService;
	  
	    @Override  
	    public boolean execute(final String action,final  org.json.JSONArray args,  
	    		final  CallbackContext callbackContext) throws org.json.JSONException {  
	        SytLog.i("orderPlugin", action);  
	        
	        cordova.getThreadPool().execute(new Runnable() {
	            public void run() {
	                try {
						execPlugin(action, args, callbackContext);
					} catch (Exception e) {
						e.printStackTrace();
						SytLog.e("AdsPlugin", "插件调用出错");
						callbackContext.error("插件调用出错");
					}
	            }
	        });
	        
	        return true;  
	  
	    }  
	    
	    private void execPlugin(final String action,final  org.json.JSONArray args,  
	    		final CallbackContext callbackContext) throws org.json.JSONException {
	        try {
	        	if (yhcService == null) {
	        		yhcService = new YHCServiceImpl();
				}
			    // 获取JS传递的args的第一个参数  
			    SytLog.i("yhcPlugin", "接收js调用"+action+"内容"); 
			    ResponseDTO responseDTO = new ResponseDTO();
			    if (action.equals("biz")) {
			    	String bizInfo = args.getString(0);
			    	String headerInfo = args.getString(1);
			    	String serviceName = args.getString(2);
					
					SytLog.i("yhcPlugin", "接收js调用"+action+"serviceName:"+serviceName+"headerInfo:"+headerInfo+"bizInfo:"+bizInfo); 
					responseDTO = yhcService.biz(serviceName, headerInfo, bizInfo);
				}
				SytLog.i("yhcPlugin","萤火虫项目返回："+ responseDTO);
				
		    	callbackContext.success(com.alibaba.fastjson.JSONObject.toJSONString(responseDTO)); 
		        return; 
			} catch (Exception e) {
				e.printStackTrace();
				SytLog.e("yhcPlugin", "调用萤火虫项目出错");
				callbackContext.success(""); 
			}  
	    }
	    
		@Override
		public void startActivityForResult(CordovaPlugin command,
				Intent intent, int requestCode) {
			
		}

		@Override
		public void setActivityResultCallback(CordovaPlugin plugin) {
			
		}

		@Override
		public Activity getActivity() {
			return this.cordova.getActivity();
		}

		@Override
		public ExecutorService getThreadPool() {
			return threadPool;
		}
	  
}
